﻿namespace SLClient.Extension
{
    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;

    public static class DesHelper
    {
        public static string DesDecrypt(string input)
        {
            if (input == null)
            {
                return "";
            }
            byte[] buffer = Convert.FromBase64String(input);
            byte[] salt = Encoding.UTF8.GetBytes("saltValue");
            AesManaged managed = new AesManaged();
            Rfc2898DeriveBytes bytes = new Rfc2898DeriveBytes("pwdValue", salt);
            managed.BlockSize = managed.LegalBlockSizes[0].MaxSize;
            managed.KeySize = managed.LegalKeySizes[0].MaxSize;
            managed.Key = bytes.GetBytes(managed.KeySize / 8);
            managed.IV = bytes.GetBytes(managed.BlockSize / 8);
            ICryptoTransform transform = managed.CreateDecryptor();
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write);
            stream2.Write(buffer, 0, buffer.Length);
            stream2.Close();
            byte[] buffer3 = stream.ToArray();
            return Encoding.UTF8.GetString(buffer3, 0, buffer3.Length);
        }

        public static string DesEncrypt(string input)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(input);
            byte[] salt = Encoding.UTF8.GetBytes("saltValue");
            AesManaged managed = new AesManaged();
            Rfc2898DeriveBytes bytes = new Rfc2898DeriveBytes("pwdValue", salt);
            managed.BlockSize = managed.LegalBlockSizes[0].MaxSize;
            managed.KeySize = managed.LegalKeySizes[0].MaxSize;
            managed.Key = bytes.GetBytes(managed.KeySize / 8);
            managed.IV = bytes.GetBytes(managed.BlockSize / 8);
            ICryptoTransform transform = managed.CreateEncryptor();
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write);
            stream2.Write(buffer, 0, buffer.Length);
            stream2.Close();
            return Convert.ToBase64String(stream.ToArray());
        }
    }
}

